package problem1567;

//1567.成绩为正数的最长子数组长度
//https://leetcode.cn/problems/maximum-length-of-subarray-with-positive-product/

class Solution {
    public int getMaxLen(int[] nums) {
        int n = nums.length;
        int[] f = new int[n+1];
        int[] g = new int[n+1];

        int ret = 0;
        for(int i = 1; i<=n; i++) {
            if(nums[i-1] > 0) {
                f[i] = f[i-1] + 1;
                g[i] = g[i-1] == 0 ? 0 : g[i-1] + 1;
            }else if(nums[i-1] < 0) {
                f[i] = g[i-1] == 0 ? 0 : g[i-1] + 1;
                g[i] = f[i-1] + 1;
            }
            ret = Math.max(ret, f[i]);
        }
        return ret;
    }
}

/*
f[i]: 以第i个元素为结尾, 乘积为正数的最长子数组长度
g[i]: 以第i个元素为结尾, 乘积为负数的最长子数组长度

nums[i] > 0:
    f[i] = f[i-1]+1;
    g[i] = g[i-1] == 0 ? 0 : g[i-1] + 1;
nums[i] < 0:
    f[i] = g[i-1] == 0 ? 0 : g[i-1] + 1;
    g[i] = f[i-1]+1;
*/